package com.imageim.yimirror.ui.fragment.base;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseFragment;
import com.imageim.yimirror.common.ui.GridItemDecoration;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.byteam.superadapter.OnItemClickListener;
import org.byteam.superadapter.SuperAdapter;

import butterknife.BindView;

/**
 * 包含recyclerView的基类
 * 用法子类集成，实现相关方法定制自己需要的样式
 * Created by Bruce.Zhou on 2018/12/25 17:54.
 * Email: 907160968@qq.com
 */
public abstract class BaseRecyclerViewFragment extends BaseFragment {

    @BindView(R.id.recycle_view)
    protected RecyclerView recycleView;
    @BindView(R.id.refresh_layout)
    protected SmartRefreshLayout refreshLayout;

    public enum RecyclerViewType {
        GRID, LINEAR
    }

    protected RecyclerViewType viewType = RecyclerViewType.GRID;

    @Override
    protected void initListener() {
        SuperAdapter adapter = getAdapter();
        if (getRecycleViewType() == RecyclerViewType.GRID) {
            GridLayoutManager layoutManager = new GridLayoutManager(getContext(), getSpanCount());
            recycleView.addItemDecoration(new GridItemDecoration(getSpanCount()));
            recycleView.setLayoutManager(layoutManager);
            recycleView.setAdapter(adapter);
        } else {

        }
        if (adapter != null) {
            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(View itemView, int viewType, int position) {
                    onAdapterItemClick(itemView, viewType, position);
                }
            });
        }

        // 刷新加载
        refreshLayout.setRefreshHeader(new MaterialHeader(getContext()));
        refreshLayout.setEnableRefresh(enableRefresh());
        refreshLayout.setEnableAutoLoadMore(true);
        refreshLayout.setEnableOverScrollBounce(true);
        refreshLayout.setEnableOverScrollDrag(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                onLoadMoreData(refreshLayout);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                onRefreshData(refreshLayout);
            }
        });
    }


    protected abstract void onRefreshData(RefreshLayout refreshLayout);

    protected abstract void onLoadMoreData(RefreshLayout refreshLayout);

    public RecyclerViewType getRecycleViewType() {
        return viewType;
    }

    public abstract SuperAdapter getAdapter();

    public int getSpanCount() {
        return 4; // default
    }

    public boolean enableRefresh() {
        return true;
    }

    public abstract void onAdapterItemClick(View itemView, int viewType, int position);

    @Override
    protected void initData() {

    }
}
